package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.DateTimeEnum;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.enums.DefaultValueEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.SubFormPoJo;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.PhoneExtEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.dictionary.ArchivedEnum;
import com.xbongbong.saas.enums.dictionary.ClueInvalidEnum;
import com.xbongbong.saas.enums.dictionary.ClueSourceEnum;
import com.xbongbong.saas.enums.dictionary.ClueStatusEnum;
import com.xbongbong.saas.enums.dictionary.PublicEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Getter
public enum TempClueEnum {
    //
    BASE_INFO(ClueEnum.BASE_INFO),
    SCORE(ClueEnum.SCORE),
    COMPANY_NAME(ClueEnum.COMPANY_NAME),
    CONTACT(ClueEnum.CONTACT),
    PHONE(ClueEnum.PHONE),
    ADDRESS(ClueEnum.ADDRESS),
    MARKET_ACTIVITY_ID(ClueEnum.MARKET_ACTIVITY_ID),
    LABEL(ClueEnum.LABEL),
    CLUE_SOURCE(ClueEnum.CLUE_SOURCE),
    CLUE_STATUS(ClueEnum.CLUE_STATUS),
    CLUE_STAGE(ClueEnum.CLUE_STAGE),
    INVALID_REASON(ClueEnum.INVALID_REASON),
    INVALID_MEMO(ClueEnum.INVALID_MEMO),
    OWNER_ID(ClueEnum.OWNER_ID),
    COUSERID(ClueEnum.COUSERID),
    EXTEND_INFO(ClueEnum.EXTEND_INFO),
    IMAGE(ClueEnum.IMAGE),
    FILE(ClueEnum.FILE),
    MEMO(ClueEnum.MEMO),
    SYSTEM_INFO(ClueEnum.SYSTEM_INFO),
    CREATOR_ID(ClueEnum.CREATOR_ID),
    ADD_TIME(ClueEnum.ADD_TIME),
    UPDATE_TIME(ClueEnum.UPDATE_TIME),
    LAST_CONNECT_TIME(ClueEnum.LAST_CONNECT_TIME),
    BACK_PUBLIC_DATE(ClueEnum.BACK_PUBLIC_DATE),
    DISTRIBUTION_DATE(ClueEnum.DISTRIBUTION_DATE),
    IS_PUBLIC(ClueEnum.IS_PUBLIC),
    IS_ARCHIVED(ClueEnum.IS_ARCHIVED),
    MARKET_ACTIVITY_NAME(ClueEnum.MARKET_ACTIVITY_NAME),
    STAGE_PROCESS_ID(ClueEnum.STAGE_PROCESS_ID),
    STAGE_ID(ClueEnum.STAGE_ID),
    STAGE_RATIO(ClueEnum.STAGE_RATIO),
    BACK_NUM(ClueEnum.BACK_NUM),
    PUBLIC_GROUP(ClueEnum.PUBLIC_GROUP),
    ;

    private ClueEnum anEnum;

    TempClueEnum(ClueEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempClueEnum> getAllEnum() {
        List<TempClueEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    public static void initBackNum(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsOpen(0);
        fieldAttrEntity.setAccuracy(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initPublicGroup(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsOpen(0);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }


    public static void initStageProcessId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsRedundant(0);
    }

    public static void initStageId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsRedundant(0);
    }

    public static void initStageRatio(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setIsRedundant(0);
        fieldAttrEntity.setIsOpen(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setAccuracy(0);
    }

    public static void initName(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.CLUE.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.CLUE.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(COMPANY_NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setCustomerNameRepeatType(BasicConstant.ONE);
    }


    public static void initClueStatus(FieldAttrEntity fieldAttrEntity) {
        ClueStatusEnum[] values = ClueStatusEnum.values();
        List<ItemPoJo> items = new ArrayList<>();
        for (ClueStatusEnum clueStatusEnum : values) {
            ItemPoJo itemPoJo = new ItemPoJo();
            if (Objects.equals(clueStatusEnum.getCode(), ClueStatusEnum.PENDING.getCode())) {
                itemPoJo.setChecked(true);
            } else {
                itemPoJo.setChecked(false);
            }
            if (Objects.equals(clueStatusEnum.getCode(), ClueStatusEnum.INVALID.getCode())) {
                List<String> fields = new ArrayList<>();
                fields.add(ClueEnum.INVALID_REASON.getAttr());
                fields.add(ClueEnum.INVALID_MEMO.getAttr());
                itemPoJo.setFieldMap(fields);
            }
            itemPoJo.setNoMove(1);
            itemPoJo.setNoDel(1);
            itemPoJo.setNoEdit(1);
            itemPoJo.setText(clueStatusEnum.getName());
            itemPoJo.setValue(clueStatusEnum.getCode());
            items.add(itemPoJo);
        }
        fieldAttrEntity.setItems(items);
        fieldAttrEntity.setComboType(ComboTypeEnum.CUSTOM.getType());
        fieldAttrEntity.setDictionaryFlag(0);
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setItemBanDefault(1);
        fieldAttrEntity.setShowEditable(0);
    }

    public static void initClueStage(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(new ArrayList<>());
    }



    public static void initIsPublic(FieldAttrEntity fieldAttrEntity) {
//        String str = "公海客户,非公海客户";
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(PublicEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initIsArchived(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ArchivedEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void   initLastConnectTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRecentTime(true);
        fieldAttrEntity.setDateType(DateTimeEnum.DATETIME.getType());
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setShowEditable(0);
    }
    
    
    public static void initBackPublicDate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType(DateTimeEnum.DATETIME.getType());
    }

    public static void initAddress(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType("");
        fieldAttrEntity.setShowDetailAddress(1);
    }

    public static void initInvalidReason(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ClueInvalidEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initClueSource(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ClueSourceEnum.class, fieldAttrEntity.getFieldType()));
        fieldAttrEntity.setDictionaryFlag(1);
    }

    public static void initMarketActivityId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(RedundantTemplateTypeEnum.MARKET_ACTIVITY.getCode());
        fieldAttrEntity.setLinkedText(MARKET_ACTIVITY_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }


    public static void initPhone(FieldAttrEntity fieldAttrEntity) {
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        FieldAttrEntity type = InitExplainsHelper.setField("text_1", "类型", "type", FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
//        String str = "工作,手机,住址,主要,传真,其他";
        List<ItemPoJo> itemPoJoList = InitExplainsHelper.setCombItemByEnum(PhoneExtEnum.class, FieldTypeEnum.COMBO.getType());
        itemPoJoList.get(0).setChecked(true);
        type.setItems(itemPoJoList);
        type.setRequired(1);
        type.setDictionaryFlag(1);
        type.setParentAttr(PHONE.anEnum.getAttr());
        items.add(type);
        FieldAttrEntity phone = InitExplainsHelper.setField("text_2", "电话", "phone", FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
        phone.setNoRepeat(1);
        phone.setParentAttr(PHONE.anEnum.getAttr());
        phone.setRequired(1);
        phone.setCustomerPhoneRepeatType(BasicConstant.ONE);
        items.add(phone);
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setDictionaryFlag(1);
        // 客户电话子表单允许开关
        fieldAttrEntity.setIsForbidden(1);
        fieldAttrEntity.setShowPublicPool(BasicConstant.ONE);
        fieldAttrEntity.setPublicPool(BasicConstant.ONE);
    }

    public static void initPhone(FieldAttrEntity fieldAttrEntity, JSONArray phoneArray) {
        SubFormPoJo subFormPoJo = new SubFormPoJo();
        List<FieldAttrEntity> items = new ArrayList<>();
        if (phoneArray.contains("subForm_1.text_1")) {
            FieldAttrEntity type = InitExplainsHelper.setField("text_1", "类型", "type", FieldTypeEnum.COMBO.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
            List<ItemPoJo> itemPoJoList = InitExplainsHelper.setCombItemByEnum(PhoneExtEnum.class, FieldTypeEnum.COMBO.getType());
            itemPoJoList.get(0).setChecked(true);
            type.setItems(itemPoJoList);
            type.setRequired(1);
            type.setDictionaryFlag(1);
            type.setParentAttr(PHONE.anEnum.getAttr());
            items.add(type);
        }
        if (phoneArray.contains("subForm_1.text_2")) {
            FieldAttrEntity phone = InitExplainsHelper.setField("text_2", "电话", "phone", FieldTypeEnum.TEXT.getType(), 4, ShowTypeEnum.ALL.getCode(), 0, 1);
            phone.setNoRepeat(1);
            phone.setParentAttr(PHONE.anEnum.getAttr());
            phone.setRequired(1);
            items.add(phone);
        }
        subFormPoJo.setItems(items);
        fieldAttrEntity.setSubForm(subFormPoJo);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setRequired(1);
        fieldAttrEntity.setDictionaryFlag(1);
        fieldAttrEntity.setIsForbidden(0);
    }

    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        //可新增权限：超级管理员、老板、销售主管、创建人、负责人、负责人主管
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setSetType(3);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(0);
        fieldAttrEntity.setMultiple(1);
        fieldAttrEntity.setShowEditable(0);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setSetType(3);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.DYNAMIC.getAlias());
        defaultAttrPoJo.setDefaultValue(DefaultValueEnum.LOGIN_USER.getValue());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initScore(FieldAttrEntity fieldAttrEntity) {
        //默认不开启
        fieldAttrEntity.setIsOpen(0);
        //默认精度 设置为 2
        fieldAttrEntity.setAccuracy(2);
        //不开启汇总
        fieldAttrEntity.setSummaryFlag(0);
    }
}
